*****************************************************************
*********** Mining and violent competition in Africa ************
****** A.S. Rigterink, T. Ghani, J.S. Lozano, J.N. Shapiro ******
*********** Replication of Figures A9 of the Appendix ***********

/*
0. Setting globals
1. Merging Delve and ML predictions
2. Graph A9

*/
*****************************************************************

clear all
version 14.0
set more off
cap log close

/*--------------------------------
------- 0. Setting globals -------
----------------------------------*/

*** Directory globals
*** NB: SET YOUR DIRECTORY HERE AND UNTEXT IF NOT RUNNING 0-Master.do
*global dir = ".\Replication_data\Stata"
*cd "$dir"

*** Subdirectories
global Do_files     "./Do"
global Data			"./Data"
global Results		"./Results"
global Tables 		"./Tables"
global Figures		"./Figures"

*** Scripts for spatially clustered standard errors
do "$Do_files/my_ols_spatial_HAC.do"
do "$Do_files/my_reg2hdfespatial.ado"

/*--------------------------------------------------
------- 1. Merging Delve and ML predictions  -------
----------------------------------------------------*/

* Reading in Machine Learning predictions
use "$Data/main.dta", replace

* Collapsing to country-year level 
collapse (rawsum) asm_voted asm_voted_gold asm_voted_2c asm_voted_3t asm_voted_diamond (mean) price_gold price_diamond price_3t price_2c (count) gid, by(gid_0 year)

* Merging to DELVE data 
merge 1:1 gid_0 year using "$Data/delve.dta", nogen

/*--------------------------
------- 2. Graph A9  -------
----------------------------*/

* Collapsing to country-year 
* NB: Delve has observations for multiple years for a small number of countries only
collapse (mean) asm_voted* delve_employment* delve_production* gid, by(gid_0)

* Correlation DELVE employment and number of ASM suitable cells (mentioned in-text)
pwcorr asm_voted delve_employment, sig 

* Generating Panel (a)
replace delve_employment = delve_employment/1000 

label var asm_voted "Number of cells predicted suitable for ASM"
label var delve_employment "Employment in ASM (x1000 persons)"

graph twoway scatter asm_voted delve_employment, /// 
msymbol(diamond) mcolor(black) mlabel(gid_0) mlabcolor(black) /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) 
graph export "$Figures/delve.pdf", replace

* Correlation DELVE employment per cell and share of cells ASM suitable (mentioned in-text)
gen asm_voted_share = asm_voted / gid 
gen delve_employment_pergid = delve_employment / gid 

* Generating Panel (b)
pwcorr asm_voted_share delve_employment_pergid, sig
label var asm_voted_share "% of cells predicted suitable for ASM"
label var delve_employment_pergid "Employment in ASM per cell (x1000 persons)"

graph twoway scatter asm_voted_share delve_employment_pergid, /// 
msymbol(diamond) mcolor(black) mlabel(gid_0) mlabcolor(black) /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) 
graph export "$Figures/delve_share.pdf", replace

* Correlation DELVE employment by commodity and share of cells ASM suitable for that commodit
for var asm_voted_*: replace X = X/gid 
for var delve_employment_* delve_production_*: replace X = X/gid/1000

* Generating Panel (c)
graph twoway scatter (asm_voted_gold delve_employment_gold), mlabel(gid_0) mlabcolor(black)mcolor(gold) ///
 || scatter (asm_voted_3t delve_employment_3t), mlabel(gid_0) mlabcolor(black) mcolor(black) ///
 || scatter (asm_voted_diamond delve_employment_diamond), mlabel(gid_0) mlabcolor(black) msymbol(diamond) mcolor(eltblue) ///
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
legend(label(1 "gold") label(2 "3t") label(3 "diamond") row(1)) /// 
xtitle("Employment in ASM per cell (x1000 persons)") ytitle("% of cells predicted suitable for ASM")
graph export "$Figures/delve_gold.pdf", replace

* Correlation gold production volume and share of cells ASM suitable for gold (mentioned in-text)
pwcorr asm_voted_gold delve_production_gold, sig

* Generating Panel (d)
graph twoway scatter (asm_voted_gold delve_production_gold), mlabel(gid_0) mlabcolor(black )mcolor(gold) ///
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) /// 
xtitle("ASM production per cell (x1000 kg)") ytitle("% of cells predicted suitable for ASM")
graph export "$Figures/delve_production_gold.pdf", replace

* Correlation for commodity-specific employment (mentioned in-text)

preserve 

keep delve_employment* asm_voted_*
drop asm_voted_share delve_employment_pergid delve_employment asm_voted_2c

rename (*gold *diamond *3t) (*1 *2 *3)
gen id=_n
reshape long asm_voted_ delve_employment_, i(id) j(temp)

pwcorr asm_voted_ delve_employment_, sig

restore
